package com.zrrd.gmall.product.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrrd.gmall.common.result.Result;
import com.zrrd.gmall.model.product.SkuInfo;
import com.zrrd.gmall.model.product.SpuImage;
import com.zrrd.gmall.model.product.SpuSaleAttr;
import com.zrrd.gmall.product.service.SkuInfoService;
import com.zrrd.gmall.rabbit.constant.MqConstant;
import com.zrrd.gmall.rabbit.service.RabbitService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/product")
public class SkuInfoController {
    @Autowired
    private SkuInfoService skuInfoService;
    @Autowired
    private RabbitService rabbitService;
    @GetMapping("/list/{page}/{limit}")
    public Result<Page<SkuInfo>> list(@PathVariable("page")Integer page,
                                      @PathVariable("limit") Integer size){
        Page<SkuInfo> pageInfo =new Page<>(page,size);
        skuInfoService.page(pageInfo,null);
        return Result.ok(pageInfo);
    }
    //上架请求
    @GetMapping("/onSale/{skuId}")
    public Result<String> onSale(@PathVariable("skuId") Long skuId){
        SkuInfo skuInfo =new SkuInfo();
        skuInfo.setId(skuId);
        skuInfo.setIsSale(1);
        //更新
        boolean update = skuInfoService.updateById(skuInfo);
        if (update){
            //发送mq消息
            rabbitService.senMessage(MqConstant.EXCHANGE_DIRECT_GOODS,
                    MqConstant.ROUTING_GOODS_UPPER,skuId);
        }
        return Result.ok("成功");
    }
    //下架
    @GetMapping("/cancelSale/{skuId}")
    public Result<String> cancelSale(@PathVariable("skuId") Long skuId){
        SkuInfo skuInfo =new SkuInfo();
        skuInfo.setId(skuId);
        skuInfo.setIsSale(0);
        //更新

        boolean update = skuInfoService.updateById(skuInfo);
        if (update){
            //发送mq消息
            rabbitService.senMessage(MqConstant.EXCHANGE_DIRECT_GOODS,
                    MqConstant.ROUTING_GOODS_LOWER,skuId);
        }
        return Result.ok("成功");
    }
    @GetMapping("/spuImageList/{spuId}")
    public Result<List<SpuImage>> spuImageList(@PathVariable("spuId") Long spuId){
        List<SpuImage> spuImageList = skuInfoService.getSpuImageList(spuId);
        return Result.ok(spuImageList);
    }
    @GetMapping("/spuSaleAttrList/{spuId}")
    public Result<List<SpuSaleAttr>> spuSaleAttrList(@PathVariable("spuId") Long spuId){
        return Result.ok(skuInfoService.getSpuSaleAttrList(spuId));
    }
    @PostMapping("/saveSkuInfo")
    public Result<String> saveSkuInfo(@RequestBody SkuInfo skuInfo){
        skuInfoService.saveSkuInfo(skuInfo);
        return Result.ok("成功");
    }

}
